package ps.server.rights;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;

import ps.util.MD5;


public class RightsManager {
    
    public static final String KEY_DEFAULT_ADMIN = "DefaultAdmin";
    
    private static String SAVE_FILE_NAME = "users.bin";
    
    User defAdminUser = new User("Admin", "");
    
    Vector<User> userList = new Vector<User>();
    
    public RightsManager() {
        load();
        
        if (Boolean.parseBoolean(System.getProperty(KEY_DEFAULT_ADMIN, "false"))) {
            User adminUser = getUserByName(defAdminUser.getName());
            if (adminUser != null) {
                userList.remove(adminUser);
            }
            defAdminUser.setAdmin(true);
            userList.add(defAdminUser);
        }
        
        if (userList.size() == 0) {
            defAdminUser.setAdmin(true);
            userList.add(defAdminUser);
        }
    }
    
    public void addUser(User user) {
        if (!existsUserName(user.getName())) {
            userList.add(user);
            save();
        }
    }
    
    public void removeUser(String userName) {
        User userToDelete = getUserByName(userName);
        if (userToDelete != null) {
            userList.remove(userToDelete);
            save();
        }
    }
    
    public User getUserWithAuthId(MD5 authId) {
        for (User user : userList) {
            if (user.getAuthId().equals(authId)) {
                return user;
            }
        }
        return null;
    }
    
    public boolean existsUserName(String userName) {
        for (User user: userList) {
            if (user.getName().equalsIgnoreCase(userName)) {
                return true;
            }
        }
        return false;
    }
    
    public User getUserByName(String userName) {
        for (User user : userList) {
            if (user.getName().equals(userName)) {
                return user;
            }
        }
        return null;
    }
    
    public void save() {
        try {
            FileOutputStream out = new FileOutputStream(SAVE_FILE_NAME);

            out.write(userList.size()); // user count
            for (User user : userList) {
                out.write(user.getAuthId().getBytes());
                out.write(user.getRight());
                out.write(user.getName().length());
                out.write(user.getName().getBytes("Cp1250"));
            }
            
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private void load() {
        try {
            if ((new File(SAVE_FILE_NAME)).exists()) {
                FileInputStream in = new FileInputStream(SAVE_FILE_NAME);
                int userCount = in.read();
                for (int i = 0; i < userCount; i++) {
                    User user = new User();
                    byte[] authId = new byte[16];
                    in.read(authId);
                    user.setAuthId(new MD5(authId));
                    user.setRight(in.read());
                    byte[] userNameBytes = new byte[in.read()];
                    in.read(userNameBytes);
                    user.setName(new String(userNameBytes, "Cp1250"));
                    userList.add(user);
                }
                in.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
